package net.yla.smartbus.module.ba.right.account.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vip.vjtools.vjkit.collection.ListUtil;
import net.yla.smartbus.core.base.PageBean;
import net.yla.smartbus.core.util.IPUtil;
import net.yla.smartbus.module.ba.right.admin.entity.AdminLoginLogsDO;
import net.yla.smartbus.module.ba.right.admin.query.LogCount;
import net.yla.smartbus.module.ba.right.admin.query.LogQuery;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import net.yla.smartbus.module.ba.right.account.mapper.LoginLogsMapper;
import net.yla.smartbus.module.ba.right.account.entity.LoginLogsDO;
import net.yla.smartbus.module.ba.right.account.service.LoginLogsService;

import java.util.ArrayList;
import java.util.List;

/**
 * @Date: 2020-08-29 11:14
 * @Author: Being Nobody
 * @Version: 1.0
 */
@Service
public class LoginLogsServiceImpl implements LoginLogsService {

    @Resource
    private LoginLogsMapper loginLogsMapper;

    @Override
    public int deleteByPrimaryKey(Integer logId) {
        return loginLogsMapper.deleteByPrimaryKey(logId);
    }

    @Override
    public int insert(LoginLogsDO record) {
        return loginLogsMapper.insert(record);
    }

    @Override
    public int insertSelective(LoginLogsDO record) {
        return loginLogsMapper.insertSelective(record);
    }

    @Override
    public LoginLogsDO selectByPrimaryKey(Integer logId) {
        return loginLogsMapper.selectByPrimaryKey(logId);
    }

    @Override
    public int updateByPrimaryKeySelective(LoginLogsDO record) {
        return loginLogsMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(LoginLogsDO record) {
        return loginLogsMapper.updateByPrimaryKey(record);
    }

    @Override
    public int logOfCount(LogCount query, long ip) {
        return loginLogsMapper.logOfCount(query, ip);
    }

    @Override
    public PageBean<JSONObject> logOfQuery(LogQuery query, long ip) {
        PageHelper.startPage(query.getPage(), query.getSize(), "logId");
        List<LoginLogsDO> objectList = loginLogsMapper.logOfQuery(query, ip);
        List<JSONObject> jsonObjects = this.disposeLog(objectList);
        PageInfo pageInfo = new PageInfo(objectList);
        if (ListUtil.isEmpty(jsonObjects)) {
            return new PageBean<JSONObject>(ListUtil.emptyList(), query.getPage(), query.getSize(), 0);
        }
        return new PageBean<>(jsonObjects, query.getPage(), query.getSize(), (int) pageInfo.getTotal());

    }

    @Override
    public JSONObject logOfView(Integer logId) {
        LoginLogsDO logsDO = loginLogsMapper.selectByPrimaryKey(logId);
        JSONObject jsonObject = null;
        if (logsDO != null) {
            jsonObject = new JSONObject();
            jsonObject.put("id", logsDO.getLogId());
            jsonObject.put("admin", logsDO.getAccountId());
            jsonObject.put("state", logsDO.getLoginState());
            jsonObject.put("url", logsDO.getUri());
            jsonObject.put("referrer", logsDO.getReferrer());
            jsonObject.put("form", logsDO.getForm());
            jsonObject.put("headers", logsDO.getHeaders());
        }
        return jsonObject;

    }

    private List<JSONObject> disposeLog(List<LoginLogsDO> objectList) {
        List<JSONObject> result = null;
        if (ListUtil.isNotEmpty(objectList) && objectList.size() > 0) {
            result = new ArrayList<>(objectList.size());
            for (LoginLogsDO logsDO : objectList) {
                JSONObject object = new JSONObject();
                object.put("id", logsDO.getLogId());
                object.put("account", logsDO.getAccountId());
                object.put("state", logsDO.getLoginState());
                object.put("url", logsDO.getReferrer());
                object.put("referrer", logsDO.getUri());
                object.put("form", logsDO.getLogId());
                object.put("headers", logsDO.getHeaders());
                object.put("time", logsDO.getCreationTime());
                result.add(object);
            }
        }
        return result;
    }

}
